//
//  NSDate+Cycledate.m
//  CycleControlFunction
//
//  Created by ddy on 2018/8/7.
//  Copyright © 2018年 cycle. All rights reserved.
//

#import "NSDate+Cycledate.h"
#import "NSCalendar+CycleCalendar.h"

@implementation NSDate (Cycledate)
/**
 * 比较当前时间nowTime和服务器传过来的Create_Time之间相差的“时分秒”
 */
- (NSDateComponents *)cycle_daltaForm:(NSDate *)createTime {
    //日历
    NSCalendar *calendar = [NSCalendar cycle_calendar];
    //日历模式
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calendar components:unit fromDate:createTime toDate:self options:0];
}
/**
 * 是否是今年
 */
- (BOOL)cycle_isThisYear {
    //日历
    NSCalendar *calendar = [NSCalendar cycle_calendar];
    //今年
    NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
    //去年
    NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
    //如果时间相同则是今年
    return nowYear = selfYear;
}
/**
 * 是否是今天
 */
- (BOOL)cycle_isToday {
    //日历
    NSCalendar *calendar = [NSCalendar cycle_calendar];
    
    //日历模式
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    //当前时间下的时间组建
    NSDateComponents *nowcmps = [calendar components:unit fromDate:[NSDate date]];
    //服务器传过来的时间对应的时间组件
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    //比较
    return (nowcmps.year == selfCmps.year && nowcmps.month == selfCmps.month && nowcmps.day == selfCmps.day);
}
/**
 * 是否是昨天
 */
- (BOOL)cycle_isYesterDay {
//    //1.日历
//    NSCalendar *calendar = [NSCalendar cycle_calendar];
//    //2.日期模式
//    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
//    //3.时间格式
//    NSDateFormatter *sdf = [[NSDateFormatter alloc]init];
//    [sdf setDateFormat:@"yyyy-MM-dd"];
//
//    //当前时间下
//    NSDate *nowDate = [NSDate date];
//    NSString *nowDateString = [sdf stringFromDate:nowDate];
//    //服务器时间
//    NSString *selfDateString = [sdf stringFromDate:self];
//
//    //4.转换成时间NSDate对象
//    NSDate *nowDater  = [sdf dateFromString:nowDateString];
//    NSDate *selfDater = [sdf dateFromString:selfDateString];
//
//    NSDateComponents *nowcmps = [calendar components:unit fromDate:nowDater];
//    NSDateComponents *selfcmps = [calendar components:unit fromDate:selfDater];
//    int absResult = abs((int)(nowcmps.day-selfcmps.day));
//    return (nowcmps.year == selfcmps.year && nowcmps.month == selfcmps.month && (absResult == 1));
    //或者
    //判断是昨天比较当前时间的年月日和服务器传过来的年月日
    NSDateFormatter *sdf = [NSDateFormatter new];
    [sdf setDateFormat:@"yyyy-MM-dd"];
    //转成只有年月日的时间
    NSDate *now_timeDate = [sdf dateFromString:[sdf stringFromDate:[NSDate date]]];
    NSDate *self_timeDate = [sdf dateFromString:[sdf stringFromDate:self]];
    //日历
    NSCalendar *calendar = [NSCalendar cycle_calendar];
    //日历模式
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    //比较
    NSDateComponents *cmps = [calendar components:unit fromDate:self_timeDate toDate:now_timeDate options:0];
    
    return cmps.year == 0 && cmps.month == 0 && cmps.day == 1;
    
    
}

/**
 * 时间戳处理 比如：str = @"12371273483"; 返回NSDate
 */
+ (NSDate *)cycle_dateFromTimerString:(NSString *)timerString{
    NSTimeInterval interval = timerString.longLongValue / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    return date;
}
/**
 * 时间戳处理 比如：str = @"12371273483"; 返回NSString
 */
+ (NSString *)cycle_dateFromTimerString:(NSString *)timerString dateFormat:(NSString *)sdf {
    NSTimeInterval interval = timerString.longLongValue / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:sdf];
    return [formatter stringFromDate:date];
}



#pragma mark --- 是否在某个时间范围内
/**
 * @brief 判断当前时间是否在fromHour和toHour之间。如，fromHour=8，toHour=23时，即为判断当前时间是否在8:00-23:00之间
 */
+ (BOOL)cycle_isBetWeenFromHour:(NSInteger)fromHour FromMinute:(NSInteger)fromMinute toHour:(NSInteger)toHour toMinute:(NSInteger)toMinute {
    
    NSDate *fromDate = [self cycle_getCustomDateWithHour:fromHour andMinute:fromMinute];
    NSDate *toDate = [self cycle_getCustomDateWithHour:toHour andMinute:toMinute];
    NSDate *currentDate = [NSDate date];
    if ([currentDate compare:fromDate] == NSOrderedDescending && [currentDate compare:toDate] == NSOrderedAscending) {
        NSLog(@"当前时间在这个范围内:%zd:%zd-%zd:%zd之间",fromHour,fromMinute,toHour,toMinute);
        return YES;
    }
    return NO;
}
+ (NSDate *)cycle_getCustomDateWithHour:(NSInteger)hour andMinute:(NSInteger)minute {
    //get currentTime
    NSDate *currentDate = [NSDate date];
    //获取日历
    NSCalendar *currentCalendar = [NSCalendar cycle_calendar];
    //日历时间类型
    NSDateComponents *currentCmps = [[NSDateComponents alloc]init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    currentCmps = [currentCalendar components:unitFlags fromDate:currentDate];
    //设置当天某个点
    NSDateComponents *resultCmps = [NSDateComponents new];
    [resultCmps setYear:[currentCmps year]];
    [resultCmps setMonth:[currentCmps month]];
    [resultCmps setDay:[currentCmps day]];
    [resultCmps setHour:hour];
    [resultCmps setMinute:minute];
    NSCalendar *resultCalendar = [NSCalendar cycle_calendar];
    return [resultCalendar dateFromComponents:resultCmps];
}

#pragma mark --- 是否在某个时间范围内(周末除外)
/**
 *  指定的时间不包括周末
 */
+ (BOOL)cycle_isNotContainWeekendBetWeenFromHour:(NSInteger)fromHour FromMinute:(NSInteger)fromMinute toHour:(NSInteger)toHour toMinute:(NSInteger)toMinute {
    //开始时间
    NSDate *fromDate = [self cycle_getCustomDateWithHour:fromHour andMinute:fromMinute];
    //结束时间
    NSDate *toDate = [self cycle_getCustomDateWithHour:toHour andMinute:toMinute];
    //当前时间
    NSDate *currentDate = [NSDate date];
    
    //判断是否是周末
    NSString *weekendString = [self cycle_weekendStringFromDate:currentDate];
    if ([@"6" isEqualToString:weekendString] || [@"7" isEqualToString:weekendString]) {
        return NO;
    }
    //判断是否是在这个时间范围内
    if ([currentDate compare:fromDate] == NSOrderedDescending && [currentDate compare:toDate] == NSOrderedAscending) {
        NSLog(@"该时间在 %ld:%ld-%ld:%ld之间！", fromHour, fromMinute, toHour, toMinute);
        return YES;
    }
    return NO;
}
///返回星期几
+ (NSString *)cycle_weekendStringFromDate:(NSDate *)inputDate {
    NSArray *weekdays = [NSArray arrayWithObjects:@"7",@"1",@"2",@"3",@"4",@"5",@"6", nil];
    //获取日历
    NSCalendar *calendar = [NSCalendar cycle_calendar];
    //获取时间空间
    NSTimeZone *timeZone = [[NSTimeZone alloc]initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone:timeZone];
    //时间组件
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:components.weekday];
}
@end
